# frozen_string_literal: true

UI.user_error!('Please run fastlane via `bundle exec`') unless FastlaneCore::Helper.bundler?

APP_SPECIFIC_VALUES = {
  wordpress: {
    display_name: 'WordPress',
    metadata_dir: 'metadata',
    glotpress_appstrings_project: 'https://translate.wordpress.org/projects/apps/android/dev/',
    glotpress_metadata_project: 'https://translate.wordpress.org/projects/apps/android/release-notes/',
    package_name: 'org.wordpress.android',
    bundle_name_prefix: 'wpandroid',
    screenshots_test_class: 'org.wordpress.android.ui.screenshots.WPScreenshotTest',
    screenshot_config_file: 'wordpress-config.json'
  },
  jetpack: {
    display_name: 'Jetpack',
    metadata_dir: 'jetpack_metadata',
    glotpress_appstrings_project: 'https://translate.wordpress.com/projects/jetpack/apps/android/',
    glotpress_metadata_project: 'https://translate.wordpress.com/projects/jetpack/apps/android/release-notes/',
    package_name: 'com.jetpack.android',
    bundle_name_prefix: 'jpandroid',
    screenshots_test_class: 'org.wordpress.android.ui.screenshots.JPScreenshotTest',
    screenshot_config_file: 'jetpack-config.json'
  }
}.freeze

UPLOAD_TO_PLAY_STORE_JSON_KEY = File.join(Dir.home, '.configure', 'wordpress-android', 'secrets', 'google-upload-credentials.json')

PROTOTYPE_BUILD_FLAVOR = 'Jalapeno'
PROTOTYPE_BUILD_TYPE = 'Debug'
PROTOTYPE_BUILD_DOMAIN = 'https://cdn.a8c-ci.services'

PROJECT_ROOT_FOLDER = File.dirname(File.expand_path(__dir__))
FASTLANE_FOLDER = File.join(PROJECT_ROOT_FOLDER, 'fastlane')
RELEASE_NOTES_SOURCE_PATH = File.join(PROJECT_ROOT_FOLDER, 'RELEASE-NOTES.txt')

VERSION_PROPERTIES_PATH = File.join(PROJECT_ROOT_FOLDER, 'version.properties')

# Instanstiate versioning classes
VERSION_CALCULATOR = Fastlane::Wpmreleasetoolkit::Versioning::MarketingVersionCalculator.new
VERSION_FORMATTER = Fastlane::Wpmreleasetoolkit::Versioning::RCNotationVersionFormatter.new
BUILD_CODE_CALCULATOR = Fastlane::Wpmreleasetoolkit::Versioning::SimpleBuildCodeCalculator.new
BUILD_CODE_FORMATTER = Fastlane::Wpmreleasetoolkit::Versioning::SimpleBuildCodeFormatter.new
VERSION_FILE = Fastlane::Wpmreleasetoolkit::Versioning::AndroidVersionFile.new(version_properties_path: VERSION_PROPERTIES_PATH)

########################################################################
# Environment
########################################################################
fastlane_require 'dotenv'
USER_ENV_FILE_PATH = File.join(Dir.home, '.wpandroid-env.default')
Dotenv.load(USER_ENV_FILE_PATH)

GITHUB_REPO = 'wordpress-mobile/WordPress-Android'
DEFAULT_BRANCH = 'trunk'
REPOSITORY_NAME = 'WordPress-Android'

# Used by Fastlane to work around the Google API random failures
ENV['SUPPLY_UPLOAD_MAX_RETRIES'] = '5'

# Fastlane's `git_branch` action and its relevant helpers use environment variables to modify
# the output. That means if we change the branch as part of an action, it'll return the incorrect
# branch. This environment variable disables this behavior: https://github.com/fastlane/fastlane/pull/21597
ENV['FL_GIT_BRANCH_DONT_USE_ENV_VARS'] = 'true'

#####################################################################################
# Version Methods
#####################################################################################

def commit_version_bump
  Fastlane::Helper::GitHelper.commit(
    message: 'Bump version number',
    files: VERSION_PROPERTIES_PATH
  )
end

# Returns the current version name from `version.properties` without needing formatting or calculations
def current_version_name
  VERSION_FILE.read_version_name
end

# Returns the release version of the app in the format `1.2` or `1.2.3` if it is a hotfix
#
def current_release_version
  # Read the current release version from the .xcconfig file and parse it into an AppVersion object
  current_version = VERSION_FORMATTER.parse(VERSION_FILE.read_version_name)
  # Return the formatted release version
  VERSION_FORMATTER.release_version(current_version)
end

#  Returns the next release version of the app in the format `1.2` or `1.2.3` if it is a hotfix
#
def next_release_version
  # Read the current release version from the .xcconfig file and parse it into an AppVersion object
  current_version = VERSION_FORMATTER.parse(VERSION_FILE.read_version_name)
  # Calculate the next release version
  next_release_version = VERSION_CALCULATOR.next_release_version(version: current_version)
  # Return the formatted release version
  VERSION_FORMATTER.release_version(next_release_version)
end

# Returns the beta version of the app in the format `1.2-rc-1`
#
def current_beta_version
  # Read the current release version from the .xcconfig file and parse it into an AppVersion object
  current_version = VERSION_FORMATTER.parse(VERSION_FILE.read_version_name)
  # Return the formatted release version
  VERSION_FORMATTER.beta_version(current_version)
end

# Returns the next beta version in the format `1.2-rc-2` (increments the build number)
#
def next_beta_version(version_name: nil)
  # Use provided version or read the current version from version.properties
  version_name ||= VERSION_FILE.read_version_name
  # Parse the version string (e.g., "1.2-rc-1") into an AppVersion object
  current_version = VERSION_FORMATTER.parse(version_name)
  # Increment the build number to get the next beta version
  next_beta_version = VERSION_CALCULATOR.next_build_number(version: current_version)
  # Format as beta version (e.g., "1.2-rc-2")
  VERSION_FORMATTER.beta_version(next_beta_version)
end

# Returns the current build code of the app
#
def current_build_code
  # Read the current build code from the version.properties file into to a BuildCode object
  current_build_code = VERSION_FILE.read_version_code
  # Return the formatted build code
  BUILD_CODE_FORMATTER.build_code(build_code: current_build_code)
end

# Returns the next build code of the app
#
def next_build_code
  # Read the current build code from the version.properties file into to a BuildCode object
  current_build_code = VERSION_FILE.read_version_code
  # Calculate the next build code
  next_build_code = BUILD_CODE_CALCULATOR.next_build_code(build_code: current_build_code)
  # Return the formatted build code
  BUILD_CODE_FORMATTER.build_code(build_code: next_build_code)
end

########################################################################
# Import domain-specific lanes
########################################################################
import 'lanes/build.rb'
import 'lanes/localization.rb'
import 'lanes/release.rb'
import 'lanes/screenshots.rb'
import 'lanes/test.rb'

default_platform(:android)

before_all do
  # Check that the env file exists
  unless is_ci || File.file?(USER_ENV_FILE_PATH)
    UI.user_error!("#{USER_ENV_FILE_PATH} not found: Please copy fastlane/env/user.env-example to #{USER_ENV_FILE_PATH} and fill in the values")
  end

  # Ensure we use the latest version of the toolkit
  check_for_toolkit_updates unless is_ci || ENV['FASTLANE_SKIP_TOOLKIT_UPDATE_CHECK']
end
